package com.imti.mall.sale.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.imti.mall.model.sms.pojo.SmsCoupon;
import com.imti.mall.model.sms.pojo.SmsCouponHistory;
import com.imti.mall.model.sms.pojo.SmsCouponProductCategoryRelation;
import com.imti.mall.model.sms.pojo.SmsCouponProductRelation;
import com.imti.mall.model.sms.vo.CouponHistoryQuery;
import com.imti.mall.model.sms.vo.CouponQuery;
import com.imti.mall.sale.dao.SmsCouponMapper;
import com.imti.mall.sale.service.SmsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SmsCouponServiceImpl extends ServiceImpl<SmsCouponMapper, SmsCoupon> implements SmsCouponService {

  @Autowired
  private SmsCouponMapper smsCouponMapper;

  @Override
  public List<SmsCoupon> getList(CouponQuery couponQuery, Integer pageNum, Integer pageSize) {
    //Integer startNum = (pageNum - 1) * pageSize + 1;
    PageHelper.startPage(pageNum, pageSize);
    return smsCouponMapper.getList(couponQuery);
  }

  @Override
  @Transactional
  public int updateCouponById(SmsCoupon smsCoupon) {
    int smsCouponResult = 0;
    int smsOtherResult = 0;

    //时间满足条件后，判断修改为全场通用、指定分类、指定商品
    if (smsCoupon.getSmsCouponProductRelationList() != null && smsCoupon.getSmsCouponProductRelationList().size() > 0) {
      //修改类型为指定商品
      smsCouponResult = smsCouponMapper.updateCouponById(smsCoupon);
      List<SmsCouponProductRelation> smsCouponProductRelationList = smsCoupon.getSmsCouponProductRelationList();
      for (SmsCouponProductRelation smsCouponProductRelation : smsCouponProductRelationList) {
        smsOtherResult = smsCouponMapper.updateProductById(smsCouponProductRelation);
      }
    } else if (smsCoupon.getSmsCouponProductCategoryRelationList() != null && smsCoupon.getSmsCouponProductCategoryRelationList().size() > 0) {
      smsCouponResult = smsCouponMapper.updateCouponById(smsCoupon);
      List<SmsCouponProductCategoryRelation> smsCouponProductCategoryRelationList = smsCoupon.getSmsCouponProductCategoryRelationList();
      for (SmsCouponProductCategoryRelation smsCouponProductCategoryRelation : smsCouponProductCategoryRelationList) {
        smsOtherResult = smsCouponMapper.updateCategoryRelationById(smsCouponProductCategoryRelation);
      }
    } else {
      smsCouponResult = smsCouponMapper.updateCouponById(smsCoupon);
    }
    if(smsCouponResult+smsOtherResult>0){
      return 1;
    }else{
      return 0;
    }
  }

  @Override
  public List<SmsCouponProductCategoryRelation> getProductCateRelationByCouponId(Long id) {
    return smsCouponMapper.getProductCateRelationByCouponId(id);
  }

  @Override
  public List<SmsCouponProductRelation> getCouponProductById(Long id) {
    return smsCouponMapper.getCouponProductById(id);
  }

  @Override
  @Transactional
  public int addCouponMsg(SmsCoupon smsCoupon){
    //添加优惠券列表sms_coupon信息
    int resultAddSmsCoupon = smsCouponMapper.addSmsCouponMsg(smsCoupon);
    int resultAddOtherCoupon = 0;
    //判断添加的属于全场通用、指定分类、指定商品
    if(smsCoupon.getSmsCouponProductRelationList()!=null && smsCoupon.getSmsCouponProductRelationList().size()>0){
      //添加类型为指定商品
      List<SmsCouponProductRelation> smsCouponProductRelationList = smsCoupon.getSmsCouponProductRelationList();
      for(SmsCouponProductRelation smsCouponProductRelation:smsCouponProductRelationList){
        resultAddOtherCoupon = smsCouponMapper.addSmsProductCoupon(smsCouponProductRelation);
      }
    }else if(smsCoupon.getSmsCouponProductCategoryRelationList()!=null && smsCoupon.getSmsCouponProductCategoryRelationList().size()>0){
      //添加商品为指定分类
      List<SmsCouponProductCategoryRelation> smsCouponProductCategoryRelationList = smsCoupon.getSmsCouponProductCategoryRelationList();
      for(SmsCouponProductCategoryRelation smsCouponProductCategoryRelation:smsCouponProductCategoryRelationList ){
        resultAddOtherCoupon = smsCouponMapper.addSmsCategoryCoupon(smsCouponProductCategoryRelation);
      }
    }
    return resultAddSmsCoupon;
  }
  @Override
  @Transactional
  public int deleteCoupon(Long id) {
    Integer useType = smsCouponMapper.getCouponById(id).getUseType();
    int deleteCouponResult = smsCouponMapper.deleteById(id);
    int deleteOtherCoupon = 0;
    if(useType==1){
      //指定分类
      deleteOtherCoupon = smsCouponMapper.deleteCategoryCouponById(id);
    }else if(useType==2){
      //指定商品
      deleteOtherCoupon = smsCouponMapper.deleteProductCouponById(id);
    }
    return deleteCouponResult+deleteOtherCoupon;
  }

  @Override
  public List<SmsCouponHistory> getHistoryList(CouponHistoryQuery couponHistoryQuery, Integer pageNum, Integer pageSize) {
    Integer startNum = (pageNum-1)*pageSize+1;
    return smsCouponMapper.getHistoryList(couponHistoryQuery,startNum,pageSize);
  }

  @Override
  public SmsCoupon getCouponById(Long id) {
    return smsCouponMapper.getCouponById(id);
  }

  @Override
  public long getHistoryCount(CouponHistoryQuery couponHistoryQuery) {
    return smsCouponMapper.getHistoryCount(couponHistoryQuery);
  }
}
